package com.jyw.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jyw.R;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/8
 */

public class FeedbackActivity extends BaseActivity implements View.OnClickListener {
    private TextView btnGo;
    private EditText editContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        findViewById(R.id.iv_back).setOnClickListener(this);
        btnGo = (TextView) findViewById(R.id.btn_go);
        editContent = (EditText) findViewById(R.id.edit_content);
        btnGo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.btn_go:
                if (editContent.getText().toString().equals("")) {
                    OkHttpManager.backgroundThreadShortToast(FeedbackActivity.this, "内容不能为空");
                } else {
                    go();
                }
                break;
        }
    }

    private void go() {
        final Request request = new Request.Builder().
                url(Server.FEEDBSCK + "?phone_data=" + getPrefs().getString("phone", "")
                        + "&content_data=" +editContent.getText().toString()
                ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(FeedbackActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                OkHttpManager.backgroundThreadShortToast(FeedbackActivity.this, "" + response.body().string());
                finish();
            }
        });
    }

}
